package com.xiangty.write;

import com.xiangty.bean.CustInfo;
import com.xiangty.bean.CustTemp;
import com.xiangty.mapper.CustInfoMapper;
import com.xiangty.mapper.CustTempMapper;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Component("custInfoWrite")
@StepScope
public class CustInfoWrite implements ItemWriter<CustTemp> {

    @Autowired
    private CustInfoMapper custInfoMapper;

    @Autowired
    private CustTempMapper custTempMapper;

    @Override
    public void write(List<? extends CustTemp> list) throws Exception {
        if (list == null) {
            System.out.println("-------------custTempWrite没有数据");
            return;
        }

//        System.out.println("----custTempWrite开始---" + list.size() + "---" + list.get(0).getId() + "-" + list.get(list.size() - 1).getId() + "---" + Thread.currentThread().getName());
//        createFile(list);

        try {
            CustInfo custInfo = null;
            for (CustTemp custTemp : list) {
                custInfo = new CustInfo();
                custInfo.setCustNo(custTemp.getCustNo());
                custInfo.setApplicationNo(custTemp.getApplicationNo());
                custInfo.setValue1(custTemp.getValue1());
                custInfo.setValue2(custTemp.getValue2());
                custInfo.setValue3(custTemp.getValue3());
                custInfo.setValue4(custTemp.getValue4());
                custInfo.setValue5(custTemp.getValue5());
                custInfo.setValue6(custTemp.getValue6());
                custInfo.setValue7(custTemp.getValue7());
                custInfo.setValue8(custTemp.getValue8());
                custInfo.setValue9(custTemp.getValue9());
                custInfo.setValue10(custTemp.getValue10());
                custInfo.setValue11(custTemp.getValue11());
                custInfo.setValue12(custTemp.getValue12());
                custInfo.setValue13(custTemp.getValue13());
                custInfo.setValue14(custTemp.getValue14());
                custInfo.setValue15(custTemp.getValue15());
                custInfo.setValue16(custTemp.getValue16());
                custInfo.setValue17(custTemp.getValue17());
                custInfo.setValue18(custTemp.getValue18());
                custInfo.setValue19(custTemp.getValue19());
                custInfo.setValue20(custTemp.getValue20());
                custInfoMapper.savecustInfo(custInfo);
//                System.out.println("------线程【" + Thread.currentThread().getName() + "】---保存----" + custTemp.getCustNo());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void createFile(List<? extends CustTemp> list) throws IOException {

        String path = "D:\\springbatchdemo\\input2\\";
        File file2 = new File(path);

        file2.delete();
        file2.mkdirs();

        File file = new File(path, list.get(0).getId() + "-" + list.get(list.size() - 1).getId() + ".txt");
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        FileOutputStream out = new FileOutputStream(file);
        // 给文件中生产30万条数据
//        long beginTime = System.currentTimeMillis();
//        System.out.println("开始时间：【 " + beginTime + " 】");
        for (CustTemp custTemp : list) {
            out.write((custTemp.getId().toString() + "\n").getBytes());
            out.flush();
        }
        out.close();
//        System.out.println("总共耗时：【 " + (System.currentTimeMillis() - beginTime) + " 】毫秒");
    }

}
